<template>
  <el-dialog title="修改密码" :visible.sync="visible" width="30%" @close="closeHandle">
    <el-row :gutter="20">
      <el-form :model="user" ref="userForm" :rules="rules"  :label-width="formLabelWidth">
        <el-form-item   prop="id">
          <el-input v-show="false"  v-model="user.id" ></el-input>
        </el-form-item>

        <el-col :span="12">
          <el-form-item label="登录名" prop="username">
            <el-input disabled  v-model="user.username" auto-complete="off"></el-input>
          </el-form-item>
        </el-col>


        <el-col :span="12">
          <el-form-item label="显示名" prop="displayName">
            <el-input disabled v-model="user.displayName" auto-complete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="密码" prop="password">
            <el-input v-model="user.password" type="password" auto-complete="off"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="确认密码" prop="checkPassword">
            <el-input v-model="user.checkPassword" type="password" auto-complete="off"></el-input>
          </el-form-item>
        </el-col>


      </el-form>
    </el-row>



    <div slot="footer" class="dialog-footer">
      <el-button @click="closeHandle">取 消</el-button>
      <el-button type="primary" @click="submitEdit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {updatePassword} from '@/api/sys/user/';

  export default {
    data() {
      var validatePass = (rule, value, callback) => {
          console.info("===="+value+"=====");
        if (value === '' || value == null || value === undefined) {
          callback(new Error('请输入密码'));
        } else {
          if (this.user.checkPassword !== '') {
            this.$refs.userForm.validateField('checkPassword');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '' || value == null || value === undefined) {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.user.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };


      return {
        visible: false,
        user:{
        },
        roles:[],
        formLabelWidth:'80px',
        rules: {
          password: [
            { validator: validatePass, trigger: 'blur' },
            {min:5,max:10,message:'密码长度大于5小于10',trigger:'change'}
          ],
          checkPassword: [
            { validator: validatePass2, trigger: 'blur' }
          ]
        },
      }
    },
    methods: {
      init(row){
        let that =this;
        this.visible =true;
        this.$nextTick(()=> {
          if(row != undefined){
            that.user = Object.assign({},row);
          }
        })
      },


      submitEdit(){
        let that = this;
        this.$refs['userForm'].validate((valid) => {
          if (valid) {
            updatePassword(that.user).then(resule => {
              let {code ,msg} = resule.data;
              if(code == '0'){
                that.$message({
                  type: 'success',
                  message: msg
                });
                that.visible = false;
              }else{
                that.$message.error(msg);
              }

            }).catch(error =>{
              that.$message.error(error.response.data.msg);
            });
          } else {

            return false;
          }
        });

      /*  assignRoles(that.user).then(function (result) {
          let {code,msg,data} = result.data;
          if(code == '0'){
            that.$message({
              type: 'success',
              message: msg
            });
            that.closeHandle();

          }else{
            that.$message.error(msg);
          }
        }).catch(function (error) {
          that.$message.error(error.response.data.msg);
        });*/

      },


      closeHandle(){
        this.visible=false;
        this.$emit('refreshDataList');

      }
    }



  }

</script>
